package com.ys.servers.versions.verticle;

import com.ys.servers.common.verticle.BasicRestfulVerticle;
import com.ys.servers.versions.data.AppType;
import com.ys.servers.versions.data.AppVersionsData;
import com.ys.servers.versions.data.AppVersionsState;
import com.ys.servers.versions.service.AppVersionsService;
import com.ys.servers.versions.service.impl.AppVersionsServiceImpl;
import io.vertx.core.Future;
import io.vertx.core.json.JsonObject;
import io.vertx.ext.web.Router;
import io.vertx.ext.web.RoutingContext;
import io.vertx.ext.web.handler.BodyHandler;
import io.vertx.serviceproxy.ProxyHelper;
/**
 * Created by taotao on 2017/5/12.
 */
public class VersionsVerticle extends BasicRestfulVerticle {

    public static final String SERVICE_NAME = "versions-service";

    private static final String VERSIONS_ADD = "/";
    private static final String VERSIONS_GET = "/:versions/:type/:state";

    private AppVersionsService appVersionsService;

    public AppVersionsService getAppVersionsService() {
        return appVersionsService;
    }

    public void setAppVersionsService(AppVersionsService appVersionsService) {
        this.appVersionsService = appVersionsService;
    }

    @Override
    public void start(Future<Void> future) throws Exception {

        super.start();
        setAppVersionsService(new AppVersionsServiceImpl());
//        ProxyHelper.registerService(AppVersionsService.class, vertx, appVersionsService, AppVersionsService.SERVICE_ADDRESS);
        final Router router = Router.router(vertx);
        // body handler
        router.route().handler(BodyHandler.create());
        // api route handler
        router.post(VERSIONS_ADD).handler(this::addAppVersionsData);
        router.get(VERSIONS_GET).handler(this::getAppVersionsData);

        String host = config().getString("versions.rest.http.address", "0.0.0.0");
        int port = config().getInteger("versions.rest.http.port", 18889);

        // create HTTP server and publish REST service
        createHttpServer(router, host, port)
                .compose(serverCreated -> publishHttpEndpoint(SERVICE_NAME, host, port))
                .setHandler(future.completer());
    }

    private void getAppVersionsData(RoutingContext context) {
        String type = context.request().getParam("type");
        String versions = context.request().getParam("versions");
        String[] vDigits = versions.split("\\.");
        if (vDigits.length == 3) {
            versions=versions+".0";
        }
        String state = context.request().getParam("state");
        AppType appType = AppType.values()[Integer.parseInt(type)-1];
        AppVersionsState appVersionsState = AppVersionsState.values()[Integer.parseInt(state)];
        getAppVersionsService().getAppVersionsData(versions, appType, appVersionsState, restfulReturnDataHandler(context));
    }

    private void addAppVersionsData(RoutingContext context) {

        JsonObject paras = context.getBodyAsJson();
        AppVersionsData appVersionsData = AppVersionsData.parseJsonObject(paras, AppVersionsData.class);
        String versions=appVersionsData.getVersions();
        String[] vDigits = versions.split("\\.");

        if (vDigits.length == 3) {
            versions=versions+".0";
            appVersionsData.setVersions(versions);
        }

      /*  String type = paras.getValue("type").toString();
        String versions = paras.getValue("versions").toString();
        String state = paras.getValue("state").toString();

        AppType appType = AppType.values()[Integer.parseInt(type)];
        AppVersionsState appVersionsState = AppVersionsState.values()[Integer.parseInt(state)];
        String url = paras.getString("url");

        boolean forceUpdate = paras.getBoolean("forceUpdate");
        boolean hotUpdate = paras.getBoolean("hotUpdate");

        String hotUpdateUrl = paras.getString("hotUpdateUrl");
        String visitsUrl = paras.getString("visitsUrl");
        String note = paras.getString("note");
        String remindUrl = paras.getString("remindUrl");
        String deploymentKey = paras.getString("deploymentKey");*/


       /* getAppVersionsService().addAppVersionsData(new AppVersionsData().setAppType(appType).setAppVersionState(appVersionsState)
                        .setVersions(versions).setDeploymentKey(deploymentKey).setForceUpdate(forceUpdate).setHotUpdate(hotUpdate)
                        .setHotUpdateUrl(hotUpdateUrl).setUrl(url).setVisitsUrl(visitsUrl).setRemindUrl(remindUrl).setNote(note)
                , restfulReturnDataHandler(context));*/

        getAppVersionsService().addAppVersionsData(appVersionsData, restfulReturnDataHandler(context));


    }

    @Override
    protected String interName() {
        return "inter-"+SERVICE_NAME;
    }

    @Override
    protected void initInternalDispatcher() {

    }
}
